package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduResourceDTO;
import com.zenith.edu.po.vo.EduResourceVO;
import com.zenith.edu.service.EduResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/resource")
@Tag(name = "课程资源管理", description = "课程资源管理接口")
public class EduResourceController {
    
    @Autowired
    private EduResourceService eduResourceService;
    
    @SecurePostMapping(value = "/createResource", roles = "ROLE_ADMIN", permission = "resource:create")
    @Operation(summary = "创建课程资源")
    public Result<EduResourceVO> createResource(@RequestBody EduResourceDTO dto) {
        EduResourceVO resourceVO = eduResourceService.createResource(dto);
        return Result.success(resourceVO);
    }
    
    @SecurePostMapping(value = "/getResourceById/{id}", roles = "ROLE_ADMIN", permission = "resource:read")
    @Operation(summary = "根据ID获取课程资源详情")
    public Result<EduResourceVO> getResourceById(@PathVariable Long id) {
        EduResourceVO resourceVO = eduResourceService.getResourceById(id);
        if (resourceVO == null) {
            return Result.fail("课程资源不存在");
        }
        return Result.success(resourceVO);
    }
    
    @SecurePostMapping(value = "/updateResource", roles = "ROLE_ADMIN", permission = "resource:update")
    @Operation(summary = "更新课程资源信息")
    public Result<EduResourceVO> updateResource(@RequestBody EduResourceDTO dto) {
        EduResourceVO resourceVO = eduResourceService.updateResource(dto);
        return Result.success(resourceVO);
    }
    
    @SecurePostMapping(value = "/deleteResource/{id}", roles = "ROLE_ADMIN", permission = "resource:delete")
    @Operation(summary = "删除指定课程资源")
    public Result<Void> deleteResource(@PathVariable Long id) {
        boolean result = eduResourceService.deleteResource(id);
        if (!result) {
            return Result.fail("课程资源删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getResourceList", roles = "ROLE_ADMIN", permission = "resource:read")
    @Operation(summary = "获取课程资源列表")
    public Result<List<EduResourceVO>> getResourceList() {
        List<EduResourceVO> resourceList = eduResourceService.getResourceList();
        return Result.success(resourceList);
    }
}